from liwe.module import LiweModule

class Stars ( LiweModule ):

	def __init__ ( self, liwe ):
		super ( Stars, self ).__init__ ( liwe, "stars" )

		self.dbm = self.liwe.dbm

	def add_ranking ( self, id_obj, module, rank, id_site = 0 ):
		"""
		add_ranking ( id_obj, module, rank, id_site )

		INPUT:
			- id_obj	- Object ID the ranking refers to
			- module	- The module name the id_obj refers to
			- rank		- obj ranking
			- id_site	- ID Site

		RETURN: 
			- id_rank
		"""
		
		dct = {
			"id_obj" : id_obj,
			"module" : module,
			"rank"	: rank,
			"id_site" : id_site,
			"uid" : self.liwe.pyhp.session.data.get ( "uid" ),
			"visible" : self.cfg.get ( "visible", "1" )
		}
		# check if update or insert
		chk = self.user_rank ( id_obj, module, id_site )
		if chk:
			self.del_rank ( chk [ 'id' ] )

		self.dbm.insert ( "stars", dct )
		id_rank = self.dbm.insert_id ()

		return id_rank

	def del_rank ( self, id ):
		"""
		del_comment ( id )

		INPUT:
			- id	- ID of the rank to be deleted

		RETURN:
			NONE
		"""

		self.dbm.delete ( "stars", "id = '%s'" % id )
		

	def del_by_id_obj ( self, id_obj, module, id_site = 0 ):
		"""
		del_by_id_obj ( id_obj, module, id_site = 0 )

		Deletes all rankings related to ``id_obj``

		INPUT:
			- id_obj
			- module
			- id_site
		"""
		conds = []
		conds.append ( "id_obj = '%s'" % id_obj )
		conds.append ( "module = '%s'" % module )
		conds.append ( "id_site = '%s'" % id_site )
		
		rows = self.dbm.select ( "stars", "id", " AND ".join ( conds ) )
		if not rows: return

		for row in rows:
			self.del_rank ( row [ 'id' ] )

	def list ( self, id_obj = "", module = "", id_site = 0 ):
		"""
		list ( id_obj, module, id_site )

		Lists all rankings related to ``id_obj``.

		"""

		conds = []
		if id_obj : s.nds.append ( "s.id_obj = '%s'" % id_obj )
		conds.append ( "s.module = '%s'" % module )
		conds.append ( "s.id_site = '%s'" % id_site )

		conds.append ( "s.uid = u.uid" )

		rows = self.dbm.all_rows ( [ "stars AS s", "user_login AS u" ],
			[ "s.id", "s.rank" , "s.uid",
			  "u.login"
			],
			" AND ".join ( conds ),
			extras = "ORDER BY s.id DESC"
		)

		return rows
	
	def user_rank ( self, id_obj = "", module = "", id_site = 0 ):
		"""
		user_rank ( id_obj, module, id_site )

		get user object ranking.

		"""
		urank = 0
		uid =  self.liwe.pyhp.session.data.get ( 'uid', '' )
		if not uid:
			res = []
			return res

		res = self.dbm.single_row ( "stars", [ "id", "rank" ], "id_obj = %s AND uid = %s AND id_site = %s" % ( id_obj, uid, id_site ) )
		if not res:
			res = []
			return res
		return res
			
	def obj_rank ( self, id_obj = "", module = "", id_site = 0 ):
		"""
		obj_rank ( id_obj, module, id_site )

		get single object ranking.

		"""

		conds = []
		if id_obj : conds.append ( "id_obj = '%s'" % id_obj )
		conds.append ( "module = '%s'" % module )
		conds.append ( "id_site = '%s'" % id_site )

		res = self.dbm.single_row ( "stars", "SUM(rank) AS sum_rank, COUNT(uid) AS tot_users", " AND ".join ( conds ), extras = "" ) 

		if not res:
			res = []
		return res

	def get_modules ( self, id_site ):
		"""
		get_modules ( id_site ) -> list of dict

		get all commented modules 
		"""
		
		res = self.dbm.all_rows ( "stars", "module", conds = "", extras = "GROUP BY module" )
		return res
	
